package com.example.zhihuijiaju;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UdpServer {

    /**
     * @param args
     */
    public static void main(String[] args) {
        while (true) {
            getMsg();
        }
    }

    /**
     * 获取信息
     */
    private static void getMsg() {
        try {
            byte[] buf = new byte[1024];
            // 一、接收数据
            // 1、创建接收数据的数据包
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            // 2.创建UPD 的 socket
            DatagramSocket socket = new DatagramSocket(12306);
            // 3、接收数据
            System.out.println("服务端开始监听！~~~~");
            socket.receive(packet);
            // 4、处理数据
            System.out.println("服务端：" + new String(buf, 0, buf.length));

            // 二、返回数据
            DatagramPacket packet2 = new DatagramPacket(buf, buf.length,
                    packet.getAddress(), packet.getPort());
            socket.send(packet2);
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}